//加载收货地址
loadAddrs();

//地址点击事件
$('.confirm-address').on('click', '.item', function () {
    $('.confirm-address .active').removeClass("active");
    $(this).addClass("active");
});

//`去结算点击事件
$('.ui-btn-theme').click(() => {
    //地址
    var addrId = $('.confirm-address .active').attr('data-id');
    if (!addrId) {
        alert("请选择一个地址！");
        return;
    }
    //购物车ID cartIds
    var carts = [];
    $.each(cartIds, (index, id) => {
        carts.push({id});
    });
    obajax.post(
        '/api/user/order',//创建一个订单 再支付
        {addrId, carts},
        (result) => {
            //成功跳转支付页面
            window.location.href="/user/order/pay?id="+result.data;

        },
        (result) => {
            alert(result.msg);
            //返回购物车
            window.location.href="/user/cart";
        }
    )
})

//根据购物车ID 获取购物车信息
function loadCarts(cartIds) {
    //加载购物车信息
    obajax.get(
        '/api/user/cart/batch',
        {cartIds},
        (result) => {
            var html =
                '<div class="confirm-goods-hd clearfix">\n' +
                '            <div class="col col1">商品</div>\n' +
                '            <div class="col col2">单价（元）</div>\n' +
                '            <div class="col col3">数量</div>\n' +
                '            <div class="col col4">小计（元）</div>\n' +
                '        </div>';
            var sum = 0;
            $.each(result.data, (index, cart) => {
                html +=
                    ' <div class="confirm-goods-bd">\n' +
                    '            <div class="goods clearfix">\n' +
                    '                <div class="col col1">\n' +
                    '                    <img src="/upload/' + cart.goods.pics[0].url + '" alt="">\n' +
                    '                    <div class="info">\n' +
                    '                        <div class="name"><a href="/goods?id=' + cart.goods.id + '">' + cart.goods.name + '</a></div>\n' +
                    '                        <div class="meta">' + cart.goods.author + '/' + cart.goods.press + '</div>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '                <div class="col col2">￥' + cart.goods.price + '</div>\n' +
                    '                <div class="col col3">' + cart.count + '</div>\n' +
                    '                <div class="col col4">￥' + cart.goods.price * cart.count + '</div>\n' +
                    '            </div>\n' +
                    '        </div>';
                sum += cart.goods.price * cart.count;
            })
            $('.confirm-goods').html(html);
            $('.item strong').text('￥' + sum);
        }
    )

}

//加载收货地址
function loadAddrs() {
    obajax.get(
        '/api/user/addr',
        {},
        (result) => {
            var html = '';
            $.each(result.data, (index, addr) => {
                if (index == 0) {
                    html += '<div class="col col-4">\n' +
                        '                <div data-id="' + addr.id + '" class="item active">';
                } else {
                    html += '<div class="col col-4">\n' +
                        '                <div data-id="' + addr.id + '" class="item">';
                }
                html +=

                    '                    <div class="action">\n' +
                    '                        <a class="edit" href="javascript:;">修改</a>\n' +
                    '                    </div>\n' +
                    '                    <div class="info">\n' +
                    '                        <div class="ellipsis"><img src="/img/ico/user.jpg" alt="" />\n' +
                    '                            <span>' + addr.contact + '</span>\n' +
                    '                        </div>\n' +
                    '                        <div class="ellipsis"><img src="/img/ico/dizhi.jpg" alt="" />\n' +
                    '                            <span>' + addr.province + '</span>\n' +
                    '                            <span>' + addr.city + '</span>\n' +
                    '                            <span>' + addr.county + '</span>\n' +
                    '                            <span>' + addr.town + '</span>\n' +
                    '                        </div>\n' +
                    '                        <div><img src="/img/ico/dizhi.jpg" alt="" />\n' +
                    '                            <span>' + addr.detail + '</span>\n' +
                    '                        </div>\n' +
                    '                        <div class="ellipsis"><img src="/img/ico/tel.jpg" alt="" />' + addr.phone + '</div>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>';
            });
            $('.confirm-address').html(html);
        }
    )
}